<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 支付响应处理
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
abstract class EGP_Payment_Respond_Abstract implements IteratorAggregate
{

    /**
     * 经过分析后的结果集
     *
     * @var array
     */
    protected $_result = array();

    /**
     * 构造方法
     *
     * @param  array  $params
     */
    public function __construct(array $params = array())
    {
        $this->parse($params);
    }

    /**
     * 分析返回的结果
     *
     * @param  array  $params
     * @return array
     */
    public function parse(array $params)
    {
        foreach ($params as $key => $value) {
            $this->_result[$key] = $value;
        }

        return $this;
    }

    /**
     * 获取所有结果
     *
     * @return array
     */
    public function getAll()
    {
        return $this->_result;
    }

    /**
     * 返回给 foreach 调用的接口
     *
     * @return ArrayObject
     */
    public function getIterator()
    {
        return new ArrayObject($this->getAll());
    }

    /**
     * 获取订单号
     *
     * @return string
     */
    abstract public function getId();

    /**
     * 获取订单总金额
     *
     * @return float
     */
    abstract public function getAmount();

    /**
     * 获取付款时间
     *
     * @return integer
     */
    abstract public function getTime();

    /**
     * 获取支付状态
     *
     * 0 - 初始状态，未付款
     * 1 - 买家已付款
     * 2 - 支付超时
     * 3 - 支付成功 (完成)
     * 4 - 支付失败 (完成)
     *
     * @return integer
     */
    abstract public function getStatus();

    /**
     * 买家是否已经付款
     *
     * @return boolean
     */
    public function isPaid()
    {
        return ($this->getStatus() === EGP_Payment::STAT_PAID);
    }

    /**
     * 卖家是否已发货
     *
     * @return boolean
     */
    public function isTimeout()
    {
        return ($this->getStatus() === EGP_Payment::STAT_TIMEOUT);
    }

    /**
     * 是否已经支付成功
     *
     * @return boolean
     */
    public function isSuccess()
    {
        return ($this->getStatus() === EGP_Payment::STAT_SUCCEED);
    }

}